//
//  ZBPresentView.m
//  礼物连击
//
//  Created by 华惠友 on 2017/9/13.
//  Copyright © 2017年 华惠友. All rights reserved.
//

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height


#import "ZBPresentView.h"

@interface ZBPresentView ()

@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic, copy) completeBlock completedBlock;
@end

@implementation ZBPresentView

- (instancetype)init {
    if (self = [super init]) {
        _originFrame = self.frame;
        [self setUI];
    }
    return self;
}

// 开始出现动画
- (void)animateWithCompleteBlock:(completeBlock)completed {
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.frame = CGRectMake(0, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
                     } completion:^(BOOL finished) {
                         // 礼物数抖起来
                         [self shakeNumberLabel];
                     }];
    
    self.completedBlock = completed;
}

- (void)shakeNumberLabel {
//    _animCount ++;
//    NSLog(@"%ld",(long)_animCount);
    
    // 再次送礼时, 将上次同一个人送的礼物动画取消 修改礼物数(在hidePresendView方法未执行前取消, 说明是连续送礼)
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidePresendView)  object:nil];
    // 延迟两秒 让送礼视图消失
    [self performSelector:@selector(hidePresendView) withObject:nil afterDelay:2];
    
    
    self.skLabel.text = [NSString stringWithFormat:@"X %ld",(long)self.giftCount];
    [self.skLabel startAnimationWithDuration:0.5];
    
    
}

// 礼物数动画结束后, 隐藏送礼动画
- (void)hidePresendView {
    [UIView animateWithDuration:0.30
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.frame = CGRectMake(SCREEN_WIDTH, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
                     } completion:^(BOOL finished) {
                         // 动画结束
                         if (self.completedBlock) {
                             self.completedBlock(finished, self.giftCount);
                         }
                         self.alpha = 0;
                         // 移除送礼视图
                         [self removeFromSuperview];
                         [self reset];
                     }];
}

- (void)removeShakeLabel {
    // 移除送礼视图
    [self removeFromSuperview];
    [self reset];
}

// 重置个属性
- (void)reset {
    self.giftCount = 0;
}

- (void)setModel:(ZBGiftModel *)model {
    _model = model;
    
    _headImageView.image = [UIImage imageNamed:model.headName];
    _giftImageView.image = [UIImage imageNamed:model.giftImageName];
    _nameLabel.text = model.name;
    _giftLabel.text = [NSString stringWithFormat:@"送了%@",model.giftName];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _headImageView.frame = CGRectMake(0, 0, self.frame.size.height, self.frame.size.height);
    _headImageView.layer.borderWidth = 1;
    _headImageView.layer.borderColor = [UIColor cyanColor].CGColor;
    _headImageView.layer.cornerRadius = _headImageView.frame.size.height / 2;
    _headImageView.layer.masksToBounds = YES;
    _giftImageView.frame = CGRectMake(self.frame.size.width - 50, self.frame.size.height - 50, 50, 50);
    _nameLabel.frame = CGRectMake(_headImageView.frame.size.width + 5, 5, _headImageView.frame.size.width * 3, 10);
    _giftLabel.frame = CGRectMake(_nameLabel.frame.origin.x, CGRectGetMaxY(_headImageView.frame) - 10 - 5, _nameLabel.frame.size.width, 10);
    
    _bgImageView.frame = self.bounds;
    _bgImageView.layer.cornerRadius = self.frame.size.height / 2;
    _bgImageView.layer.masksToBounds = YES;
    
    _skLabel.frame = CGRectMake(CGRectGetMaxX(self.frame) + 5,-20, 50, 30);
}

- (void)setUI {
//    self.layer.cornerRadius = self.frame.size.height / 2.0;
//    self.layer.masksToBounds = YES;
    _bgImageView = [[UIImageView alloc] init];
    _bgImageView.backgroundColor = [UIColor blackColor];
    _bgImageView.alpha = 0.3;
    
    _headImageView = [[UIImageView alloc] init];
    _giftImageView = [[UIImageView alloc] init];
    _nameLabel = [[UILabel alloc] init];
    _giftLabel = [[UILabel alloc] init];
    _nameLabel.textColor  = [UIColor whiteColor];
    _nameLabel.font = [UIFont systemFontOfSize:13];
    _giftLabel.textColor  = [UIColor yellowColor];
    _giftLabel.font = [UIFont systemFontOfSize:13];
    
    // 初始化动画label
    _skLabel =  [[ZBShakeLabel alloc] init];
    _skLabel.font = [UIFont systemFontOfSize:16];
    _skLabel.borderColor = [UIColor yellowColor];
    _skLabel.textColor = [UIColor greenColor];
    _skLabel.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:_bgImageView];
    [self addSubview:_headImageView];
    [self addSubview:_giftImageView];
    [self addSubview:_nameLabel];
    [self addSubview:_giftLabel];
    [self addSubview:_skLabel];
}

- (void)dealloc {
    NSLog(@"delloc");
}

@end
